<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head th:replace="common/fragment::top">

</head>

<body>
<div class="lyear-layout-web">
    <div class="lyear-layout-container">
        <!--左侧导航-->
        <div th:replace="common/fragment::leftmenu"></div>
        <!--End 左侧导航-->

        <!--头部信息-->
        <div th:replace="common/fragment::header"></div>
        <!--End 头部信息-->

        <!--页面主要内容-->
        <main class="lyear-layout-content">
            <div class="container-fluid">
                <div col-md-4>
                    <label for="sellchoice">销售订单</label>
                    <select id="sellchoice" name="sell">
                        <option value="-999">请选择</option>
                        <option th:each="sellpo:${selllist}" th:text="${sellpo.name}"
                                th:value="${sellpo.id}">请选择
                        </option>
                    </select>
                </div>
            </div>
            <!--销售单详情-->
            <!-- 默认空表格-->
            <th:block>
                <div class="row show-grid loopsell" th:id="tab-999">
                    <div class="col-md-3">
                        <label class="col-md-4 " style="margin: 5px 0">创建日期</label>
                        <span class="form-control" th:text="----创建日期----"></span>
                    </div>
                    <div class="col-md-3">
                        <label class="col-md-4" style="margin: 5px 0">要货日期</label>
                        <span class="form-control" th:text="----要货日期----"></span>
                    </div>
                    <div class="col-md-3">
                        <label class="col-md-4" style="margin: 5px 0">责任人</label>
                        <span class="form-control" th:text="----责任人----"></span>
                    </div>
                </div>
            </th:block>
            <!-- END默认空表格-->
            <!-- 销售单表格 循环-->
            <th:block th:each="selltab:${selllist}">
                <div class="row show-grid loopsell" hidden="hidden" th:id="tab+${selltab.name}">
                    <div class="col-md-3">
                        <label class="col-md-4" style="margin: 5px 0">创建日期</label>
                        <span class="form-control" th:text="${#dates.format(selltab.createTime, 'yyyy-MM-dd')}"></span>
                    </div>
                    <div class="col-md-3">
                        <label class="col-md-4" style="margin: 5px 0">要货日期</label>
                        <span class="form-control" th:text="${#dates.format(selltab.need, 'yyyy-MM-dd')}"></span>
                    </div>
                    <div class="col-md-3">
                        <label class="col-md-4" style="margin: 5px 0">责任人</label>
                        <span class="form-control" th:text="${selltab.userPO.username}"></span>
                    </div>
                </div>
            </th:block>
            <!-- END销售单表格 循环-->
            <!--END销售单详情-->

            <!--添加出库明细-->
            <!--添加设定阈值按钮组-->
            <div class="form-group col-md-10">
                <div class="col-md-2">
                    <button
                            class="btn-xs btn-info ajax-post col-md-12"
                            id="config-detail-btn"
                            type="button">添加明细
                    </button>
                </div>

                <div class="col-md-2" hidden="hidden" id="reset-detail-btn-div">
                    <button
                            class="btn-xs btn-info ajax-post col-md-12"
                            id="reset-detail-btn"
                            type="button">重置
                    </button>
                </div>
                <div class="col-md-2" hidden="hidden" id="save-detail-btn-div">
                    <button
                            class="btn-xs btn-info ajax-post col-md-12"
                            id="save-detail-btn"
                            type="button">保存出库单
                    </button>
                </div>

            </div>
            <!--添加出库明细-->
            <!--添加明细-->
            <div hidden="hidden" id="detail-table">
                <table class="table table-bordered">
                    <thead id="goodheader">
                    <tr>
                        <!--                <th>序号</th>-->
                        <th hidden="hidden">明细id</th>
                        <th hidden="hidden" >商品id</th>
                        <th>商品名称</th>
                        <th>商品型号</th>
                        <!--    <th>角色图标</th>-->
                        <th>商品规格</th>
                        <!--                                            <th>包装</th>-->
                        <th>计划销售量</th>
                        <th>已出库</th>
                        <th>现有库存</th>
                        <th>出库数量</th>
                        <!--    <th>操作</th>-->
                    </tr>
                    </thead>
                    <tbody class="hiddendetail" th:each="sell:${selllist}" th:id="detailtable+${sell.name}">
                    <!--/*@thymesVar id="sellDetailVOList" type="com.examplemartin.jpademo.pojo.vo.sellchase.PurDetailVO"*/-->
                    <tr th:each="detail:${sell.sellDetailVOList}">
                        <td hidden="hidden" th:text="${detail.id ne null?detail.id:''}">明细id</td>
                        <td hidden="hidden"  class="goodid" th:text="${detail.goodPO.id}">
                            商品id
                        </td>
                        <td th:text="${detail.goodPO.name}">商品名称</td>
                        <td th:text="${detail.goodPO.spec}">商品型号</td>
                        <!--    <th>角色图标</th>-->
                        <td th:text="${detail.goodPO.unit}">商品规格</td>
                        <!--                                            <th>包装</th>-->
                        <td class="goodcount" th:text="${detail.goodcount}">计划销售量</td>
                        <td class="outStockCount" th:text="${detail.outStockCount}"></td>
                        <td class="currCountInstock" th:text="${repomap[detail.goodPO.id]}"></td>
                        <td><input class="form-control input-sm incount" placeholder="出库数量" type="number"></td>
                    </tr>

                    </tbody>
                </table>
            </div>
            <!--END添加明细-->
        </main>
        <!--End 页面主要内容-->
    </div>
</div>

<div th:insert="common/fragment::tail-script"></div>

<style>
    .modify {
        color: #ac2925;
        font-weight: bolder;
    }

    .hiddendetail {
        display: none;
    }

    .changedetail {
        color: #7A56D4;
        font-weight: bolder;
    }
</style>

<script type="text/javascript">
    $(function () {
        let flag = true


// 选择-->销售订单内容切换
        $('#sellchoice').change(function () {
            let tablabel = ''
            let val = $('#sellchoice  option:selected').text()
            if (val.indexOf('请选择') == 0) {
                tablabel = 'tab' + "-999"
            } else {
                tablabel = "tab" + val
            }
            console.log('#' + tablabel)

            $('.loopsell').attr('hidden', 'hidden')
            $('#' + tablabel).attr('hidden', false)
        });
// 选择-->销售订单内容切换

// 打开明细
        $('#config-detail-btn').click(function () {

            // 选择的标注
            let tablabel = ''
            let val = $('#sellchoice  option:selected').text()
            if (val.indexOf('请选择') == 0) {
                tablabel = 'tab' + "-999"
                confirmMsg("请选择销售订单")
                return false;

            } else {
                tablabel = "tab" + val
            }
            // END选择的标注
            // 判断是否选中了订单

            flag = !flag
            if (flag == false) {
                $('#sellchoice').attr('disabled', true)
                $('#sellchoice').css('color', '#6A5ACD')
                $('#config-detail-btn').text('重新选择订单')
                $('.loopsell span').css('color', '#6A5ACD')
                // 打开保存按钮
                $('#save-detail-btn-div').attr('hidden', false)
                // 打开订单明细表
                // 明细表标题栏
                $('#detail-table').attr('hidden', false)
                // 明细表内容
                let t = 'detailtable' + val
                $('#detail-table tbody').addClass('hiddendetail')
                $('#' + t).removeClass('hiddendetail')


            } else {
                $('#sellchoice').attr('disabled', false)
                $('#sellchoice').css('color', 'black')
                $('#config-detail-btn').text('添加明细')
                $('.loopsell span').css('color', 'black')
                // 隐藏保存按钮
                $('#save-detail-btn-div').attr('hidden', true)

                // 关闭订单明细表
                // 关闭明细表标题栏
                $('#detail-table').attr('hidden', true)
                // 明细表内容
                let t = 'detailtable' + val
                $('#detail-table tbody').addClass('hiddendetail')
                // 清空输入
                $('#detail-table tbody input').each(function () {
                    $(this).val('')
                })


            }

        })
// END明细

// 输入数量 为整数
        $('.incount').each(function (i, e) {
            $(e).blur(function () {
                let val = parseInt($(e).val())

                if (val == null || val == "Null" || val == '' || isNaN(val)) {
                    $(this).val(null)
                    return
                }
                if (val === 'NaN') {
                    $(this).val(null)
                    return
                }

                if (parseInt(val) < 0) {
                    confirmMsg("输入单价有误")
                    return false;
                }

                if (parseInt(val) > 0) {
                    $(e).addClass('changedetail')
                }
                $(this).val(val)

            });
        });

// 保存订单
        $('#save-detail-btn').click(function () {
            let save_flag = true;
            let data = null
            let save_msg = ''

            let sellid = $('#sellchoice').val()
            let sellPO = {id: sellid}
            // 停机明细
            let detailtable = ''
            let val = $('#sellchoice  option:selected').text()
            if (val.indexOf('请选择') == 0) {
                detailtable = 'detailtable' + "-999"
            } else {
                detailtable = "detailtable" + val
            }

            // 判断是否已超出销售订单数量
            $('#'+detailtable).children('tr').each(function (i,e) {
                let goodcount = $(e).children('.goodcount').text()
                let outStockCount = $(e).children('.outStockCount').text()
                let incount = $(e).find('.incount').val()
                let currCountInstock = $(e).find('.currCountInstock').text()

                if(parseInt(currCountInstock)<parseInt(incount)){
                    save_flag=false
                    confirmMsg("现有库存不足")
                }

                if(parseInt(goodcount)<(parseInt(outStockCount)+parseInt(incount)))
                {
                    save_msg = "出库数量超出订单"
                    confirmMsg(save_msg)
                    save_flag=false
                    return false
                }else{
                    // 循环有变化的数值
                    let sellOutDetailPOList = []
                    $('#'+detailtable).children('tr').each(function (i,e) {
                        // console.log($(e).children('.goodid').text())
                        let val = $(e).find('.incount').val()

                        if(val>0){
                            let val = parseInt($(e).find('.incount').val())
                            let goodid = $(e).children('.goodid').text()

                            let goodPO = {id:goodid}
                            let SellOutDetailPO = {
                                goodPO:goodPO,
                                goodcount:val
                            }
                            sellOutDetailPOList.push(SellOutDetailPO)
                        }
                    });
                    // 生成json
                    data = {
                        sellPO:sellPO,
                        sellOutDetailPOList:sellOutDetailPOList
                    }
                    // // 发送ajax
                    // post_ajax("/sellout/savesellout.ajax",data,"/sellout/selloutstock.do")
                }
                if(save_flag==false){
                    return false;
                }

            })



            if(save_flag == true){

                //发送ajax
                post_ajax("/sellout/savesellout.ajax",data,"/sellout/selloutstock.do")
            }
        });
    });
</script>
</body>
</html>